package com.fuluwa.domain.product.model;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.StringUtils;

import com.fuluwa.domain.file.model.FileInfo;
import com.fuluwa.domain.shared.AbstractIdMantainceDomainObject;
import com.fuluwa.domain.usercenter.model.UserProfile;
import com.fuluwa.infra.consts.LucenceConsts;
import com.fuluwa.infra.exception.PersistenceException;
import com.google.common.collect.Lists;

@Entity
@Table(name = "p_product")
public class Product extends AbstractIdMantainceDomainObject {

	public static enum ProductStatus {
		UNPUBLISH, ONLINE, OFFLINE
	}

	private String productName;
@ManyToOne
	private Category category;// 类别
	@Column(name="t_price")
	private double price;// 价

	private String keywords;// 关键字,separated by ,

	@Lob
	private String introduction;// 产品介绍

	private ProductStatus status = ProductStatus.UNPUBLISH; // 已发布/未发布/下架

	// private String customization;//自定义属性

	// private String attribute;//类别属性

	@ManyToOne
	private FileInfo fileInfo;

	@ManyToOne
	private UserProfile userProfile;

	// @Column(name="f_desc")
	// private String desc;

	@Temporal(TemporalType.TIMESTAMP)
	private Date publishedTime;// 发布时间
	
	private boolean indexed=false;

	private Product() {
	}

	public Product(Long id) {
		super(id);
	}

	public Product(String productName, Category category, double price, String keywords, String introduction,
			FileInfo fileInfo, UserProfile userProfile) {
		this.productName = productName;
		this.category = category;
		this.price = price;
		this.keywords = keywords;
		this.introduction = introduction;
		this.fileInfo = fileInfo;
		this.userProfile = userProfile;
	}

	public String getProductName() {
		return productName;
	}

	public Category getCategory() {
		return category;
	}

	public double getPrice() {
		return price;
	}

	public String getKeywords() {
		return keywords;
	}

	public String getIntroduction() {
		return introduction;
	}

	public ProductStatus getStatus() {
		return status;
	}

	public FileInfo getFileInfo() {
		return fileInfo;
	}

	public UserProfile getUserProfile() {
		return userProfile;
	}

	public Date getPublishedTime() {
		return publishedTime;
	}

	public void offline() throws PersistenceException {
		status = ProductStatus.OFFLINE;
		update();
	}

	public void publish() throws PersistenceException {
		status = ProductStatus.ONLINE;
		this.publishedTime = new Date();
		update();
	}

	public void updateProductName(String productName) throws PersistenceException {
		this.productName = productName;
		update();
	}

	public void updateCategory(Category category) throws PersistenceException {
		this.category = category;
		update();
	}

	public void updateKeywords(String keywords) throws PersistenceException {
		this.keywords = keywords;
		update();
	}

	public void updateIntroduction(String introduction) throws PersistenceException {
		this.introduction = introduction;
		update();
	}
	
	public Map<String,Object>  toSolrMap(){
		Map<String,Object>  ret=new HashMap<String, Object>();
		try {
			ret.put(LucenceConsts.Product.AUTHOR_NAME, userProfile.getUserBasicInfo().getChineseName());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ret.put(LucenceConsts.Product.CATEGORY, category.getName());
		if(category.getParnet()!=null)
		ret.put(LucenceConsts.Product.CATEGORY_PARENT, category.getParnet().getName());
		
		try {
			if(!fileInfo.isIndexed()) {
				ret.put(LucenceConsts.Product.CONTENT, fileInfo.loadAsString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ret.put(LucenceConsts.Product.INTRODUCTION, introduction);
		ret.put(LucenceConsts.Product.KEYWORDS,Lists.newArrayList(keywords.split(",")));
		ret.put(LucenceConsts.Product.PRICE, price);
		ret.put(LucenceConsts.Product.NAME, productName);
		return ret;
	}

	public String getUnquieId() {
		return getClass().getCanonicalName()+LucenceConsts.SEPERATE+id;
	}
	
	public Product load(String unquieId) throws PersistenceException {
		String id=StringUtils.substringAfter(unquieId, LucenceConsts.SEPERATE);
		this.id=Long.valueOf(id);
		return this.load();
	}
	public void updateIndexed() throws PersistenceException {
		indexed=true;
		update();
	}
}
